<?php
namespace jy_commission;
defined('BY_JYA') or exit('error');
/*
	分销佣金
	
*/
class commission extends \plugin{
	/*
		生成佣金
	*/
	function create($order,$isbuy_jiesuan=0){
		if($isbuy_jiesuan){
			$this->setting['isbuy_jiesuan']=1;
		}
		if($this->setting['is_nomerch'] && $order['merchid']){
			return;
		}
		//启用《无极乾坤分销》
		if($this->plugin_exists('jy_feewu')){
			$jy_feewuset=plugin('jy_feewu')->plugin_setting('basic');
			if($jy_feewuset['open']){
				model('jy_feewu/commission')->create($order);
				return;
			}
		}
		//启用《排位轮次分销》
		if($this->plugin_exists('jy_paiweifee')){
			$jy_paiweifee=plugin('jy_paiweifee')->plugin_setting('basic');
			if($jy_paiweifee['open']){
				model('jy_paiweifee/team')->upteam($order);
			}
		}
		$setlevel=intval($this->setting['level']);
		if($setlevel==0){
			return;
		}
		$this->order=$order;
		$member=$this->get('member',array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']),array('mid','is_agent','agent_id','level_id'));
		if(!$member['mid']){
			$member=$this->model('member')->checkmember();
		}else{
			if(!$member['is_agent'] && ($this->setting['become']==2 || $this->setting['become']==3 || $this->setting['become']==4)){
				$this->model('member')->checkmember();
			}
		}
		$this->model('model')->buy_creditfee($order,$member);
		//分销保护期
		if($member['agent_id']){
			$isbao_time=$this->model('member')->upbao_time($order['mid']);
		}
		if($member['level_id']){
			$nowlevel=$this->getcolumn('level',array('id'=>$member['level_id'],'uniacid'=>$_SESSION['uniacid']),'level');
		}else{
			$nowlevel=0;
		}
		$this->nowlevelid=$member['level_id'];
		$this->nowlevel=$nowlevel;
		$this->ismaxlevel=$nowlevel;
		$minlevel=$this->get('level',array('uniacid'=>$_SESSION['uniacid']),'','','level asc');
		$minlevel=$minlevel['level'];
		$maxlevel=$this->get('level',array('uniacid'=>$_SESSION['uniacid']),'','','level desc');
		$maxlevel=$maxlevel['level'];
		$this->commission_order['maxlevel']=$maxlevel;
		//本人是分销商，启用内购
		if($this->setting['self_commission'] && $member['is_agent']){
			$agent_id=$member['mid'];
		}else{
			$agent_id=$member['agent_id'];
		}
		//启用《补货机制》
		if($this->plugin_exists('jy_buhuo')){
			$this->jy_buhuo=plugin('jy_buhuo')->plugin_setting('basic');
		}
		//自购升级
		if($member['is_agent']){
			$this->update('member',array('myorder_price +='=>$order['price']),array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
			$this->model('member')->update_agent_level($order['mid'],$order['id']);
			if($this->jy_buhuo['isopen']){
				model('jy_buhuo/model')->addorder($order,'jy_weishop');
			}
		}
		//现在支付方式分佣
		if($this->setting['pay_type'] && $order['pay_id']){
			$paylog=pdo_get('core_paylog',array('id'=>$order['pay_id'],'uniacid'=>$_SESSION['uniacid']),array('pay_type','di_credit','di_fee','fee'));
			if($paylog['di_fee']>0){
				$paylog['di_credit']=$paylog['di_credit']=='credit2'?'credit':$paylog['di_credit'];
				if(strpos($this->setting['pay_type'],';'.$paylog['di_credit'].';') !== false){
					$this->order['price'] -= $paylog['di_fee'];
				}
				if(strpos($this->setting['pay_type'],';'.$order['paytype'].';') !== false){
					$this->order['price'] -= $paylog['fee']-$paylog['di_fee'];
				}
				//两种支付都不支持
				if(strpos($this->setting['pay_type'],';'.$paylog['di_credit'].';') !== false && strpos($this->setting['pay_type'],';'.$order['paytype'].';') !== false){
					return;
				}
			}elseif(strpos($this->setting['pay_type'],';'.$order['paytype'].';') !== false){
				return;
			}
		}
		if($this->order['price']<=0){
			return;
		}
		if($this->order['plugin_name']=='jy_shopxianhua'){
			$addfee=0;
			foreach($this->order['goods'] as $goods){
				if($goods['optionid']){
					$goodsprice=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$goods['optionid'],'uniacid'=>$_SESSION['uniacid']),'price');
				}else{
					$goodsprice=pdo_getcolumn('jy_weishop_goods',array('id'=>$goods['goodsid'],'uniacid'=>$_SESSION['uniacid']),'price');
				}
				$goodsfee=$goods['total']*$goodsprice;
				$addfee +=$goods['total_price']-$goodsfee;
			}
			$this->order['price'] -=$addfee;
		}
		$this->commission_order=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$this->order['mid'],
				'ordersn'=>$this->order['ordersn'],
				'order_price'=>$this->order['price'],
				'plugin_name'=>$this->order['plugin_name']?:'',
				'commission_money'=>0,
				'ping_money'=>0,
				'ji_chaomoney'=>0,
				'ji_money'=>0,
				'ji_price'=>0,
				'area_money'=>0,
				'xuni_money'=>0,
				'jifen'=>0,
				'yuefee1'=>$this->order['price'],
				'yuefee2'=>$this->order['price'],
				'yearfee'=>$this->order['price'],
				'createtime'=>TIMESTAMP,
		);
		//生成虚拟币
		if($member['agent_id']){
			//生成社区代理
			$this->up_areafee($order,$member['agent_id']);
			$this->up_xunifee($order,$member['agent_id']);
		}
		//判断有没有佣金权限
		if($order['plugin_name']=='jy_pingdd_pin'){
			$iscan=model('jy_pingdd_pin/model')->checkcanfee($order['mid']);
			if(!$iscan){
				return false;
			}
		}
		//佣金计算方式，是否扣除运费
		if($this->setting['commissionfeetype']==1){
			$this->commission_order['commission_price']=$this->order['price'];
		}elseif($this->setting['commissionfeetype']==3){
			$this->commission_order['commission_price']=$this->order['price'] + $this->order['deductcredit'] + $this->order['deductcredit2'];
		}else{
			$this->commission_order['commission_price']=$this->order['price']-$this->order['dispatchprice'];
		}
		//购销差额
		if($this->setting['jicha_open'] && $this->setting['jicha_type']==1){
			$level=$this->get('level',array('id'=>$member['level_id'],'uniacid'=>$_SESSION['uniacid']));
			$this->create_redcommission($member,$level,0,true);
		}
		if($this->setting['jicha_type']==2){
			//会员折扣价
			$level_de_price=$this->order['level_de_price'];
			//file_put_contents(ROOT_D.'/1.TXT',$level_de_price."\n",FILE_APPEND);
			if($level_de_price>0){
				$this->commission_order['ji_money']+=$level_de_price;
			}
		}
		//开启价差奖
		if($this->setting['jicha_price']){
			$level_de_price=$this->setting['jicha_type']==2?$level_de_price:$this->order['level_de_price'];;
			if($level_de_price>0){
				$this->commission_order['ji_price']=$level_de_price;
			}
		}
		if($this->setting['jicha_one']){
			$levelnum=$setlevel;
		}else{
			$levelnum=12;
		}
		//当分销层级大于0时，逐次寻找上级推荐人并计算佣金
		$this->jicha_pingone=array();
		for($i=0;$i<$levelnum;$i++){
			if(!$agent_id){
				break;
			}
			$member=$this->get('member',array('mid'=>$agent_id,'uniacid'=>$_SESSION['uniacid']));
			$agent_id=$member['agent_id'];
			if(($i || $this->setting['jicha_ping0']) && $member['level_id']==$this->nowlevelid){
				$isping=1;
			}else{
				$isping=0;
			}
			if($member['is_agent'] && !$member['stop']){
				$this->create_commission($member,$i+1,$isping);
			}
			//如果他的上级是我
			$ta_agent_id=$this->getcolumn('member',array('mid'=>$member['agent_id'],'uniacid'=>$_SESSION['uniacid']),'agent_id');
			if(($agent_id>0 && $ta_agent_id==$agent_id) || $agent_id==$member['mid']){
				break;
			}
			$this->nowlevelid=$member['level_id'];
		}
		//生成分销订单
		$this->commission_order['yuefee1']=$this->commission_order['yuefee1']<0?0:$this->commission_order['yuefee1'];
		$this->commission_order['yuefee2']=$this->commission_order['yuefee2']<0?0:$this->commission_order['yuefee2'];
		$this->commission_order['yearfee']=$this->commission_order['yearfee']<0?0:$this->commission_order['yearfee'];
		$indata=$this->commission_order;
		unset($indata['ji_money']);
		unset($indata['ji_price']);
		unset($indata['ping_money']);
		unset($indata['ji_chaomoney']);
		unset($indata['maxlevel']);
		unset($indata['isarea']);
		$this->insert('order',$indata);
		if($order['sendtype']==3){
			$status=pdo_getcolumn('jy_weishop_order',array('id'=>$order['id'],'uniacid'=>$_SESSION['uniacid']),'status');
			if($status==3){
				$this->order_complete($order);
			}
		}
		return true;
	}
	/*
		给指定会员的计算佣金
		统一佣金比例佣金计算比较简单
		但是商品独立佣金设置该当如何计算呢？
		首先遍历订单的所有商品
			逐个商品检查是否存在独立佣金规则，如果存在，则将订单中该商品的金额单独拿出来计算佣金，订单金额剩余部分在统一计算佣金
	*/
	function create_commission($member,$commission_level=1,$isping=0){
		if($member && $member['is_agent'] && $member['stop']==0){
			//相应会员存在且是分销商且状态正常			
			if(!$member['level_id']){
				//获取默认等级佣金比例
				$level_commissions=$this->plugin_setting('default_level');
				$level_id=0;
				$level['level']=0;
				$commission_per=$level_commissions['commission'.$commission_level].'%';
			}else{
				if($commission_level==1 && $this->setting['levelfee_type']==1){
					$level=$this->get('level',array('id'=>$member['level_id'],'uniacid'=>$_SESSION['uniacid']));
					$this->onelevel=$level;
				}elseif($this->setting['levelfee_type']==1){
					$level=$this->onelevel;
				}elseif($this->setting['levelfee_type']!=1){
					$level=$this->get('level',array('id'=>$member['level_id'],'uniacid'=>$_SESSION['uniacid']));
				}
				$level_commissions=json_decode($level['commissions'],true);
				$level_id=$level['id'];
				$commission_per=$level_commissions['commission'.$commission_level].'%';
				$this->ismaxlevel=$level['level']>$this->ismaxlevel?$level['level']:$this->ismaxlevel;
			}
			$level_de_price=0;
			$onelevel_de_price=0;
			//平级-按当前会员VIP价
			if($isping && $this->setting['jicha_pingfee'] && $this->ismaxlevel<=$level['level']){
				if((!$this->jicha_pingone[$level_id] && $this->setting['jicha_pingone']==1) || !$this->setting['jicha_pingone']){
					$level_de_price=model('jy_weishop/activity')->get_level_de_price($this->order,true,$member['mid']);
					if($level_de_price>0 && $this->commission_order['commission_price']+$this->order['level_de_price']>0){
						$onelevel_de_price=$level_de_price/($this->commission_order['commission_price']+$this->order['level_de_price']);
					}
				}
			}
			$ji_per=$level_commissions['ji_rate'].'%';
			$ping_per=$level_commissions['ping_rate'];
			$ji_chao=$level_commissions['ji_chao'];
			$money=0;
			$ji_money=0;
			$ping_money=0;
			$ji_chaomoney=0;
			$order_price=$this->commission_order['commission_price'];
			$remark=array(
				'level_id'=>$level_id,
				'commission_level'=>$commission_level,
				'order_price'=>$order_price,
				'goods_commission'=>array(),
			);
			
			//先检查是否存在商品独立佣金规则，如果存在则对订单相应商品部分金额单独计算佣金
			$total_lastcostprice=0;
			//非按笔计算
			$this->needorder=false;
			if($this->setting['get_buyordernum']!=1){
				foreach($this->order['goods'] as $goods){
					if(!$goods['id']){
						$this->needorder=true;
						continue;	
					}
					if(!$goods['fee_desc'] && $goods['merchid']){
						model('jy_weishop/order')->setfee_desc($goods['orderid']);
					}
					$total_price=$goods['total_price']?$goods['total_price']:pdo_getcolumn('jy_weishop_order_goods',array('id'=>$goods['id'],'uniacid'=>$_SESSION['uniacid']),'total_price');
					if($goods['optionid']){
						$shopgoods=pdo_get('jy_weishop_goods_option',array('id'=>$goods['optionid'],'uniacid'=>$_SESSION['uniacid']),array('costprice'));
						$shopgoods['pcate']=pdo_getcolumn('jy_weishop_goods',array('id'=>$goods['goodsid'],'uniacid'=>$_SESSION['uniacid']),'pcate');
					}else{
						$shopgoods=pdo_get('jy_weishop_goods',array('id'=>$goods['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('pcate','costprice'));
					}
					$costprice=$shopgoods['costprice'];
					$total_costprice=$goods['total']*$costprice;
					$total_lastcostprice +=$total_costprice;
					$goods_commission=$this->get('goods',array('goods_id'=>$goods['goodsid'],'uniacid'=>$_SESSION['uniacid']));
					if(!$goods_commission['id'] && $shopgoods['pcate']){
						$goods_commission=$this->get('goods',array('goods_pcate'=>$shopgoods['pcate'],'uniacid'=>$_SESSION['uniacid']));
					}
					//
					if($goods_commission['yuefee_close']){
						$this->commission_order['yuefee1'] -=$total_price;
					}
					if($goods_commission['yuefee_close2']){
						$this->commission_order['yuefee2'] -=$total_price;
					}
					if($goods_commission['yearfee_close']){
						$this->commission_order['yearfee'] -=$total_price;
					}
					if($goods_commission['jicha_close']){
						$this->commission_order['ji_price'] +=$total_price;
					}
					/*if($this->jy_buhuo['isopen']){
						$buhuocheck=model('jy_buhuo/model')->uptotal($member['mid'],$goods['goodsid'],$goods['total'],'jy_weishop');
						if(!$buhuocheck){
							$order_price=$order_price-$goods['total_price'];
							continue;
						}
					}*/
					if(!$goods_commission['id']){
						$this->needorder=true;
						continue;
					}
					if($goods_commission['ignore']){
						$order_price=$order_price-$goods['total_price'];
						continue;
					}
					//如果商品单独设置佣金，将减少本商品的金额
					$order_price=$order_price-$total_price;
					$goods_commission['commissions']=$goods_commission['commissions']?json_decode($goods_commission['commissions'],true):array();
					foreach($goods_commission['commissions'] as $gc){
						if($gc['level_id']==0){
							$demogoods_commission_per=$gc['commission'.$commission_level];
							$demogoods_ji_per=$gc['ji_rate'];
							$demogoods_ping_per=$gc['ping_rate'];
							$demogoods_ji_chao=$gc['ji_chao'];
						}
						if($gc['level_id']!=$level_id){
							continue;
						}else{
							$goods_commission_per=$gc['commission'.$commission_level];
							$goods_ji_per=$gc['ji_rate'];
							$goods_ping_per=$gc['ping_rate'];
							$goods_ji_chao=$gc['ji_chao'];
							break;
						}
					}
					$goods_commission_per=$goods_commission_per?:$demogoods_commission_per;
					//$goods_commission_per=$goods_commission_per?:$commission_per;
					$goods_ji_per=$goods_ji_per?:$demogoods_ji_per;
					$goods_ping_per=$goods_ping_per?:$demogoods_ping_per;
					$goods_ji_chao=$goods_ji_chao?:$demogoods_ji_chao;
					if($goods_commission['ping_close']){
						$goods_ping_per=0;
					}
					if($goods_commission['chao_close']){
						$goods_ji_chao=0;
					}
					//利润计算
					if($this->setting['commissionfeetype']==2){
						$total_price -=$total_costprice;
						$total_lastcostprice -=$total_costprice;
					}
					if(substr($goods_commission_per,-1)=='%'){
						$goods_commission_money=($total_price*substr($goods_commission_per,0,-1))/100;
						$money+=$goods_commission_money;
						$goods_ji_money=($total_price*substr($goods_ji_per,0,-1))/100;
					}else{
						$goods_commission_money=$goods_commission_per*$goods['total'];
						$money+=$goods_commission_money;
						$goods_ji_money=$goods_ji_per*$goods['total'];
					}
					if(substr($goods_ji_per,-1)=='%'){
						$goods_ji_money=($total_price*substr($goods_ji_per,0,-1))/100;
					}else{
						$goods_ji_money=$goods_ji_per*$goods['total'];
					}
					$ji_money+=$goods_ji_money;
					if(substr($goods_ping_per,-1)=='%'){
						$nowvidprice=$total_price-$onelevel_de_price*$total_price;
						$goods_ping_money=($nowvidprice*substr($goods_ping_per,0,-1))/100;
					}else{
						$goods_ping_money=$goods_ping_per*$goods['total'];
					}
					$ping_money+=$goods_ping_money;
					if(substr($goods_ji_chao,-1)=='%'){
						$goods_ji_chaomoney=($total_price*substr($goods_ji_chao,0,-1))/100;
					}else{
						$goods_ji_chaomoney=$goods_ji_chao*$goods['total'];
					}
					$ji_chaomoney+=$goods_ji_chaomoney;
					$remark['goods_commission'][]=array('ordergoodsid'=>$goods['id'],'goods_id'=>$goods['goodsid'],'total_price'=>$total_price,'total'=>$goods['total'],'commission_per'=>$goods_commission_per,'commission_money'=>$goods_commission_money);
				}
			}
			if($this->needorder || count($this->order['goods'])<=0){
				$commission_per=substr($commission_per,0,-1);
				$ji_per=substr($ji_per,0,-1);
				if($order_price>0 && $this->setting['get_buyordernum']!=1){
					//利润计算
					if($this->setting['commissionfeetype']==2){
						$order_price -=$total_lastcostprice;
					}
					$order_commission_money=intval($order_price*$commission_per)/100;
					$money+=$order_commission_money;
					$remark['order_commission']=array('order_price'=>$order_price,'commission_per'=>$commission_per,'commission_money'=>$order_commission_money);
				}elseif($order_price>0 && $this->setting['get_buyordernum']==1){//按笔分销
					$order_commission_money=$commission_per;
					$money+=$order_commission_money;
					$remark['order_commission']=array('order_price'=>$order_price,'commission_per'=>$commission_per,'commission_money'=>$order_commission_money);
				}
				$order_ji_money=intval($order_price*$ji_per)/100;
				$ji_money+=$order_ji_money;
				if($this->setting['jicha_pingfee']){
					$nowvidprice=($order_price+$this->order['level_de_price'])-$onelevel_de_price*($order_price+$this->order['level_de_price']);
					$order_ping_money=intval($nowvidprice*$ping_per)/100;
				}else{
					$order_ping_money=intval($order_price*$ping_per)/100;
				}
				$ping_money+=$order_ping_money;
				if(substr($ji_chao,-1)=='%'){
					$goods_ji_chaomoney=($order_price*substr($ji_chao,0,-1))/100;
				}else{
					$goods_ji_chaomoney=$ji_chao;
				}
				$ji_chaomoney+=$goods_ji_chaomoney;
			}
			$remark['commission_money']=$money;
			//分销等级大于设置的，分销佣金归零，保留极差分红
			if($commission_level>$this->setting['level']){
				$money=0;
			}
			if(!$this->setting['jicha_open'] || $this->setting['jicha_type']==1 ||($this->setting['jicha_one'] && $commission_level>1)){
				$ji_money=0;
			}elseif($this->setting['jicha_type']==2){
				$ji_money=0;
				$level_de_price=$level_de_price?:model('jy_weishop/activity')->get_level_de_price($this->order,true,$member['mid']);
				$ji_money+=$level_de_price;
			}
			$ji_money=$ji_money-$this->commission_order['ji_money'];
			//取最大值
			if($this->setting['jicha_gettype']){
				$nowmoney=$money;
				if($ji_money<=$nowmoney){
					$ji_money=0;
				}else{
					$money=0;
				}
			}
			if($money>0){
					$jifen=0;
					if($this->setting['jifenrate']){					
						$jifen=$this->setting['jifenrate']*$money/100;
						$money=$money-$jifen;
						$jifenrateup=$this->setting['jifenrateup']?:1;
						$jifen=$jifen*$jifenrateup;
					}
					$this->commission_order['jifen']+=$jifen;
					$this->commission_order['commission_money']+=$money;
					$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$member['mid'],
						'level'=>$commission_level,
						'ordersn'=>$this->order['ordersn'],
						'order_price'=>$this->order['price'],
						'money'=>$money,
						'jifen'=>$jifen?:0,
						'remark'=>ijson_encode($remark),
						'createtime'=>TIMESTAMP,
						'status'=>$this->setting['isbuy_jiesuan']?1:0,
					);
					$this->insert('commission',$new_commission);
					//model('jy_weishop/notice')->notice_fenxiao_feeadd($member['mid'],$new_commission['money']);
			}
			if($ji_money>0){
					$this->commission_order['commission_money']+=$ji_money;
					$this->commission_order['ji_money']+=$ji_money;
					$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$member['mid'],
						'level'=>$commission_level,
						'ordersn'=>$this->order['ordersn'],
						'order_price'=>$this->order['price'],
						'yuefee1'=>$this->commission_order['yuefee1'],
						'yuefee2'=>$this->commission_order['yuefee2'],
						'yearfee'=>$this->commission_order['yearfee'],
						'money'=>$ji_money,
						'jifen'=>0,
						'remark'=>ijson_encode($remark),
						'createtime'=>TIMESTAMP,
						'status'=>$this->setting['isbuy_jiesuan']?1:0,
						'type'=>20,
					);
					$this->insert('commission',$new_commission);
			}
			if($isping && $ping_money>0 && $this->ismaxlevel<=$level['level'] && !$this->jicha_pingone[$level_id]){
					$this->commission_order['commission_money']+=$ping_money;
					$this->commission_order['ping_money']+=$ping_money;
					$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$member['mid'],
						'level'=>$commission_level,
						'ordersn'=>$this->order['ordersn'],
						'order_price'=>$this->order['price'],
						'money'=>$ping_money,
						'jifen'=>0,
						'remark'=>ijson_encode($remark),
						'createtime'=>TIMESTAMP,
						'status'=>$this->setting['isbuy_jiesuan']?1:0,
						'type'=>26,
					);
					$this->insert('commission',$new_commission);
					if($this->setting['jicha_pingone']){
						$this->jicha_pingone[$level_id]=1;
					}
			}
			//开启价差奖
			if($this->setting['jicha_price']){
				$level_de_price=$level_de_price?:model('jy_weishop/activity')->get_level_de_price($this->order,true,$member['mid']);
				$jicha_price=$level_de_price-$this->commission_order['ji_price'];
				if($this->setting['jicha_pricejian']){
					$jicha_price -=$this->commission_order['ping_money'];
				}
				if($this->setting['jicha_pricejian2']){
					$jicha_price -=$this->commission_order['ji_chaomoney'];
				}
				if($jicha_price>0){
					$this->commission_order['ji_price']=$level_de_price;
					$this->commission_order['commission_money']+=$jicha_price;
					$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$member['mid'],
						'level'=>$commission_level,
						'ordersn'=>$this->order['ordersn'],
						'order_price'=>$this->order['price'],
						'money'=>$jicha_price,
						'jifen'=>0,
						'remark'=>ijson_encode($remark),
						'createtime'=>TIMESTAMP,
						'status'=>$this->setting['isbuy_jiesuan']?1:0,
						'type'=>29,
					);
					$this->insert('commission',$new_commission);
				}
			}
			//开启超级奖
			if($this->setting['jicha_chao'] && $commission_level==1 && $level['level']<$this->nowlevel){
				if($ji_chaomoney>0){
					$this->commission_order['commission_money']+=$ji_chaomoney;
					$this->commission_order['ji_chaomoney']+=$ji_chaomoney;
					$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$member['mid'],
						'level'=>$commission_level,
						'ordersn'=>$this->order['ordersn'],
						'order_price'=>$this->order['price'],
						'money'=>$ji_chaomoney,
						'jifen'=>0,
						'remark'=>ijson_encode($remark),
						'createtime'=>TIMESTAMP,
						'status'=>$this->setting['isbuy_jiesuan']?1:0,
						'type'=>28,
					);
					$this->insert('commission',$new_commission);
				}
			}
			//此处判断分销商是否满足升级条件，如果满足则升级分销商等级
			$member_update=array(
				'order_price'=>$member['order_price']+$this->commission_order['order_price'],
				'order_num'=>$member['order_num']+1,
			);
			if($commission_level==1){
				$member_update['order_price_next']=$member['order_price_next']+$this->commission_order['order_price'];
				$member_update['order_num_next']=$member['order_num_next']+1;
			}
			$this->nowlevel=$level['level'];
			$this->update('member',$member_update,array('mid'=>$member['mid']));
			$this->model('member')->update_agent_level($member['mid']);
		}
		return $member['agent_id'];
	}
	/*
		级差分佣type=1;
	*/
	function create_redcommission($agent,$level=array(),$i=0,$isgetfee=false){
			if(!$agent['level_id']){
				//获取默认等级分红比例
				$level_commissions=$this->plugin_setting('default_level');
				$level_id=0;
				$commission_per=$level_commissions['ji_rate'].'%';
			}else{
				$level['commissions']=json_decode($level['commissions'],true);
				$level_id=$level['id'];
				$commission_per=$level['commissions']['ji_rate'].'%';
			}
			$money=0;
			$order_price=$this->commission_order['order_price'];
			$remark=array(
				'level_id'=>$level_id,
				'order_price'=>$order_price,
				'goods_commission'=>array(),
			);
			if($this->order['goods']){
			foreach($this->order['goods'] as $goods){
					$total_price=$goods['total_price']?$goods['total_price']:pdo_getcolumn('jy_weishop_order_goods',array('id'=>$goods['id'],'uniacid'=>$_SESSION['uniacid']),'total_price');
					$goods_commission=$this->get('goods',array('goods_id'=>$goods['goodsid']));
					if(!$goods_commission['id']){
						$shopgoods=pdo_get('jy_weishop_goods',array('id'=>$goods['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('pcate','costprice'));
						$goods_commission=$this->get('goods',array('goods_pcate'=>$shopgoods['pcate'],'uniacid'=>$_SESSION['uniacid']));
					}
					if($goods_commission['ignore'] || !$goods_commission['id']){
						if($goods_commission['ignore']){
							$order_price=$order_price-$total_price;
						}
						//只要有一个商品，没有独立设置分销，就订单计算佣金
						$this->needorder=true;
						continue;
					}
					//如果商品单独设置佣金，将减少本商品的金额
					$order_price=$order_price-$total_price;
					$goods_commission['commissions']=$goods_commission['commissions']?json_decode($goods_commission['commissions'],true):array();
					foreach($goods_commission['commissions'] as $gc){
						if($gc['level_id']==0){
							$demogoods_commission_per=$gc['ji_rate'];
						}
						if($gc['level_id']!=$level_id){
							continue;
						}else{
							$goods_commission_per=$gc['ji_rate'];
							break;
						}
					}
					$goods_commission_per=$goods_commission_per?:$demogoods_commission_per;
					//$goods_commission_per=$goods_commission_per?:$commission_per;	
					if(substr($goods_commission_per,-1)=='%'){
						$goods_commission_money=($total_price*substr($goods_commission_per,0,-1))/100;
					}else{
						$goods_commission_money=$goods_commission_per*$goods['total'];
					}
					$money+=$goods_commission_money;
			}
			}
			if($order_price>0 || ($this->order['goods'] && $this->needorder)){
				$commission_per=substr($commission_per,0,-1);
				$order_commission_money=intval($order_price*$commission_per)/100;
				$money+=$order_commission_money;
				$remark['order_commission']=array('order_price'=>$order_price,'commission_per'=>$commission_per,'commission_money'=>$order_commission_money);
			}
			$remark['commission_money']=$money;
			$money=$money-$this->commission_order['ji_money'];
			if($money>0){
				$this->commission_order['ji_money']+=$money;
			}
			if($money>0 && !$isgetfee){
				$this->commission_order['commission_money']+=$money;
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$agent['mid'],
					'level'=>$i,
					'ordersn'=>$this->order['ordersn'],
					'order_price'=>$this->order['price'],
					'money'=>$money,
					'jifen'=>0,
					'remark'=>ijson_encode($remark),
					'createtime'=>TIMESTAMP,
					'status'=>$this->setting['isbuy_jiesuan']?1:0,
					'type'=>20,
				);
				$this->insert('commission',$new_commission);
			}
			//判断是否存在团队长
			$this->commission_order['isarea']=true;
			//返回上级
			return $agent['agent_id'];
	}
	/*
		社区代理
	*/
	public function up_areafee($order,$agent_id=0){
		if(!$agent_id){
			return false;
		}
		if($this->setting['area_open']!=1){
			return false;
		}
		$address=json_decode($order['address'],true);
		if($address && $address['name'] && $address['pcd']){
			if(!is_array($address['pcd'])){
				$address['pcd']=json_decode($address['pcd'],true);
			}			
		}else{
			$address=pdo_get('core_address',array('id'=>$order['addressid']));
			$address['pcd']=json_decode($address['pcd'],true);
		}
		$area_code=$this->model('model')->get_area_code($address['pcd']);
		if(!$area_code){
			return false;
		}
		$order_price=$order['price'];
		$list=$this->getall('member',array('area_code'=>$area_code,'is_agent'=>1,'uniacid'=>$_SESSION['uniacid']));
		foreach($list as $item){
			$money=0;
			foreach($order['goods'] as $goods){
					$total_price=$goods['total_price']?:pdo_getcolumn('jy_weishop_order_goods',array('id'=>$goods['id'],'uniacid'=>$_SESSION['uniacid']),'total_price');
					$goods_commission=$this->get('goods',array('goods_id'=>$goods['goodsid']));
					if(!$goods_commission['id']){
						$shopgoods=pdo_get('jy_weishop_goods',array('id'=>$goods['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('pcate','costprice'));
						$goods_commission=$this->get('goods',array('goods_pcate'=>$shopgoods['pcate'],'uniacid'=>$_SESSION['uniacid']));
					}
					if($goods_commission['ignore'] || !$goods_commission['id']){
						$goods_commission=array();
						if($goods_commission['ignore']){
							$order_price=$order_price-$total_price;
						}
						//只要有一个商品，没有独立设置分销，就订单计算佣金
						$this->needorder=true;
						continue;
					}
					$goods_commission['commissions']=$goods_commission['commissions']?json_decode($goods_commission['commissions'],true):array();
					foreach($goods_commission['commissions'] as $gc){
						if($gc['level_id']!=$item['level_id'] || !$gc['area_open']){
							continue;
						}else{
							$goods_commission_per=$gc['area_rate'];
							$goods_xuni_per=$gc['area_xunirate'];
							break;
						}
					}
					if(substr($goods_commission_per,-1)=='%'){
						if(count($order['goods'])==1){
							$total_price=$order_price;
						}
						$goods_commission_money=($total_price*substr($goods_commission_per,0,-1))/100;
						$money +=$goods_commission_money;
					}else{
						$goods_commission_money=$goods_commission_per*$goods['total'];
						$money +=$goods_commission_money;
					}
					if(substr($goods_xuni_per,-1)=='%'){
						if(count($order['goods'])==1){
							$total_price=$order_price;
						}
						$goods_xuni_money=($total_price*substr($goods_xuni_per,0,-1))/100;
						$xuni_money +=$goods_xuni_money;
					}else{
						$goods_xuni_money=$goods_xuni_per*$goods['total'];
						$xuni_money +=$goods_xuni_money;
					}
					//如果商品单独设置佣金，将减少本商品的金额
					$order_price=$order_price-$total_price;
			}
			if($item['level_id'] && $this->needorder){
				$level=$this->get('level',array('id'=>$item['level_id'],'uniacid'=>$_SESSION['uniacid']));
				if($level['area_open']!=1 || !$level['area_rate']){
					continue;
				}else{
					if(substr($level['area_rate'],-1)=='%'){
						$money +=($order['price']*substr($level['area_rate'],0,-1))/100;
					}else{
						$money +=$level['area_rate'];
					}
					if(substr($level['area_xunirate'],-1)=='%'){
						$xuni_money +=($order['price']*substr($level['area_xunirate'],0,-1))/100;
					}else{
						$xuni_money +=$level['area_xunirate'];
					}
				}
			}
			$this->commission_order['area_money']=$money;
			if($money>0){
				$new_commission=array(
							'uniacid'=>$_SESSION['uniacid'],
							'mid'=>$agent_id,
							'level'=>16,
							'ordersn'=>$order['ordersn'],
							'order_price'=>$order['price'],
							'money'=>$money,
							'createtime'=>TIMESTAMP,
							'status'=>$this->setting['isbuy_jiesuan']?1:0,
				);
				$this->insert('commission',$new_commission);
			}
			if($xuni_money>0){
				$this->commission_order['xuni_money'] +=$xuni_money;
				$new_commission=array(
							'uniacid'=>$_SESSION['uniacid'],
							'mid'=>$agent_id,
							'level'=>19,
							'ordersn'=>$order['ordersn'],
							'order_price'=>$order['price'],
							'money'=>$xuni_money,
							'createtime'=>TIMESTAMP,
							'isxuni'=>1,
							'status'=>$this->setting['isbuy_jiesuan']?1:0,
				);
				if($this->setting['isbuy_jiesuan'] && $this->setting['credit_nojie']){
					$new_commission['status']=2;
					$new_commission['complete_time']=TIMESTAMP;
					model('core/member')->member_credit($agent_id,'credit'.$this->setting['credit_type'],$xuni_money,true,'jy_commission','社区代理奖励');
				}
				$this->insert('commission',$new_commission);
			}
		}
	}
	/*
		虚拟币
	*/
	public function up_xunifee($order,$agent_id){
		if(!$agent_id){
			return false;
		}
		if($this->setting['xuni_open']!=1){
			return false;
		}
		$order_price=$order['price'];
		$money=0;
		$member=$this->get('member',array('mid'=>$agent_id));
		foreach($order['goods'] as $goods){
					$total_price=$goods['total_price']?:pdo_getcolumn('jy_weishop_order_goods',array('id'=>$goods['id'],'uniacid'=>$_SESSION['uniacid']),'total_price');
					$goods_commission=$this->get('goods',array('goods_id'=>$goods['goodsid']));
					if(!$goods_commission['id']){
						$shopgoods=pdo_get('jy_weishop_goods',array('id'=>$goods['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('pcate','costprice'));
						$goods_commission=$this->get('goods',array('goods_pcate'=>$shopgoods['pcate'],'uniacid'=>$_SESSION['uniacid']));
					}
					if($goods_commission['ignore'] || !$goods_commission['id']){
						$goods_commission=array();
						if($goods_commission['ignore']){
							$order_price=$order_price-$total_price;
						}
						//只要有一个商品，没有独立设置分销，就订单计算佣金
						$this->needorder=true;
						continue;
					}
					$goods_commission['commissions']=$goods_commission['commissions']?json_decode($goods_commission['commissions'],true):array();
					foreach($goods_commission['commissions'] as $gc){
						if($gc['level_id']!=$member['level_id']){
							continue;
						}else{
							$goods_commission_per=$gc['xuni_rate'];
							break;
						}
					}
					if(substr($goods_commission_per,-1)=='%'){
						if(count($order['goods'])==1){
							$total_price=$order_price;
						}
						$goods_commission_money=($total_price*substr($goods_commission_per,0,-1))/100;
						$money +=$goods_commission_money;
					}else{
						$goods_commission_money=$goods_commission_per*$goods['total'];
						$money +=$goods_commission_money;
					}
					//如果商品单独设置佣金，将减少本商品的金额
					$order_price=$order_price-$total_price;
		}
		if($member['level_id'] && $this->needorder){
				$level=$this->get('level',array('id'=>$member['level_id'],'uniacid'=>$_SESSION['uniacid']));
				$level['commissions']=json_decode($level['commissions'],true);
				if(!$level['commissions']['xuni_rate']){
				}else{
					if(substr($level['commissions']['xuni_rate'],-1)=='%'){
						$money +=($order['price']*substr($level['commissions']['xuni_rate'],0,-1))/100;
					}else{
						$money +=$level['commissions']['xuni_rate'];
					}
				}
		}
		$this->commission_order['xuni_money']=$money;
		if($money>0){
				$new_commission=array(
							'uniacid'=>$_SESSION['uniacid'],
							'mid'=>$agent_id,
							'level'=>18,
							'ordersn'=>$order['ordersn'],
							'order_price'=>$order['price'],
							'money'=>$money,
							'createtime'=>TIMESTAMP,
							'isxuni'=>1,
							'status'=>$this->setting['isbuy_jiesuan']?1:0,
				);
				if($this->setting['isbuy_jiesuan'] && $this->setting['credit_nojie']){
					$new_commission['status']=2;
					$new_commission['complete_time']=TIMESTAMP;
					model('core/member')->member_credit($agent_id,'credit'.$this->setting['credit_type'],$money,true,'jy_commission','直推奖励');
				}
				$this->insert('commission',$new_commission);
		}
	}
	public function upgroupaddfee($order=array()){
		if(!$order['mid']){
			return 0;
		}
		$agent_id=$this->getcolumn('member',array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']),'agent_id');
		$ordergoods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$order['id'],'uniacid'=>$_SESSION['uniacid']),array('goodsid','id','total'));
		for($i=0;$i<12;$i++){
			if(!$agent_id){
				break;
			}
			$money=0;
			foreach($ordergoods as $goods){
				$goods['id']=$goods['goodsid'];
				$addfee=$this->model('model')->getgroupaddfee($goods,$order['mid'],$agent_id,true);
				$goods['total']=$goods['total']?:0;
				$money +=$addfee*$goods['total'];
			}
			if($money>0){
					$new_commission=array(
								'uniacid'=>$_SESSION['uniacid'],
								'mid'=>$agent_id,
								'level'=>25,
								'ordersn'=>$order['ordersn'],
								'order_price'=>$order['price'],
								'money'=>$money,
								'createtime'=>TIMESTAMP,
								'status'=>$this->setting['isbuy_jiesuan']?1:0,
					);
					$this->insert('commission',$new_commission);
			}
			$agent_id=$this->getcolumn('member',array('mid'=>$agent_id,'uniacid'=>$_SESSION['uniacid']),'agent_id');
		}
		return true;
	}
	/*
		订单完成时，把佣金状态由初始的订单待完成升级为待结算
	*/
	public function order_complete($order){
		$list=$this->getall('commission',array('ordersn'=>$order['ordersn'],'status'=>0));
		$this->update('commission',array('status'=>1,'complete_time'=>TIMESTAMP),array('ordersn'=>$order['ordersn'],'status'=>0,'uniacid'=>$_SESSION['uniacid']));
		$order['api_data']=$order['api_data']?json_decode($order['api_data'],true):array();
		$commission_money_types2=$this->commission_money_types2;
		foreach($list as $info){
			if(($info['level']==18 || $info['level']==19) && $this->setting['credit_nojie']){
				$this->update('commission',array('status'=>2,'complete_time'=>TIMESTAMP),array('id'=>$info['id'],'uniacid'=>$_SESSION['uniacid']));
				model('core/member')->member_credit($info['mid'],'credit'.$this->setting['credit_type'],$info['money'],true,'jy_commission',$commission_money_types2[$info['level']]);
				continue;
			}
			if($info['jifen']>0){
				if($order['api_data']['merch_dai']==1 && $order['merchid']){
					$merch=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['merchid']));
					if($merch['mid']){
						$member=model('core/member')->detail($merch['mid'],array('credit1','credit2','credit3','credit4','credit6','mobile','avatar','nickname'));
						if($member['credit1']<$info['jifen']){
							continue;
						}
						$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$info['mid']),array('username','nickname'));
						$nickname=$member['nickname']?:$member['username'];
						model('core/member')->member_credit($merch['mid'],'credit1',$info['jifen'],false,'jy_merch','商户扣积分（送['.$nickname.']分销积分）');
					}else{
						continue;
					}
				}
				model('core/member')->add_credit1($info['mid'],$info['jifen'],'佣金兑积分','jy_commission');
			}
			$jy_area_agent = $this->plugin_exists('jy_area_agent');
			if($jy_area_agent){
				model('jy_area_agent/member')->koufenfee($info);
			}
		}
	}
	public function order_cancel($order){
		$this->update('commission',array('status'=>4,'complete_time'=>TIMESTAMP),array('ordersn'=>$order['ordersn'],'status'=>array(0,1),'uniacid'=>$_SESSION['uniacid']));
		$this->update('order',array('status'=>4),array('ordersn'=>$order['ordersn'],'status'=>array(0,1),'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
	/*
		创建结算单
	*/
	public function create_balance($mid,$post=array()){
		if(!$mid){
			exi('请先选择要结算的会员！','error');
		}
		if(!$post['money'] || $post['money']<=0){
			exi('请输入提现额度！','error');
		}
		if($post['pay_type']=='downline'){
			if(!$post['wechat']){
				exi('请先填写微信号！','error');
			}
			if(!$post['mobile']){
				exi('请先填写联系电话！','error');
			}
		}
		if($post['pay_type']=='alipay'){
			if(!$post['realname']){
				exi('请先填写真实姓名！','error');
			}
			if(!$post['alipay']){
				exi('请先填写支付宝帐号！','error');
			}
			if(!$post['alipay1']){
				exi('请确认帐号','error');
			}
			if($post['alipay1']!=$post['alipay']){
				exi('确认帐号与支付宝帐号不匹配！','error');
			}
			$typetitle='支付宝';
		}
		if($post['pay_type']=='union'){
			if(!$post['realname']){
				exi('请输入姓名','error');
			}
			if(!$post['codesn'] && !$post['iscodesn'] && !$_SESSION['wxapp_id']){
				exi('请输入身份证号','error');
			}
			if(!$post['bankname'] && !$post['isapibank']){
				exi('请输入所属银行','error');
			}
			if(!$post['bankcard']){
				exi('请银行卡号','error');
			}
			if(!$post['bankcard1'] && !$post['isapibank']){
				exi('请确认卡号','error');
			}
			if($post['bankcard']!=$post['bankcard1'] && !$post['isapibank']){
				exi('确认卡号与银行卡号不匹配！','error');
			}
			$typetitle='银行卡';
		}
		//获取可结算佣金
		$member=pdo_get('jy_commission_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		$money=$post['money'];
		if($money>$member['money']){
			exi('提现额度不能超过可提现总额！','error');
		}
		//结算最低佣金检测
		if($money<$this->setting['balance_money']){
			exi('提现额度不能低于'.$this->setting['balance_money'].'！','error');
		}
		if($this->setting['balance_difee']){
			$lastcredit=pdo_getcolumn('core_members',array('id'=>$mid),'credit1');
			if($lastcredit<$money){
				exi('提现需扣除等量积分，您当前积分不足，仅剩'.$lastcredit.'！','error');
			}else{
				cfc('core')->member_credit($mid,'credit1',$money,false,'jy_commission','提现扣除等量积分');
			}
		}
		$new_balance=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'balancesn'=>get_ordersn($this->tablename('commission'),'balancesn','AC'),
			'commission_num'=>$commission_num,
			'money'=>$money,
			'pay_type'=>$post['pay_type'],
			'realname'=>$post['realname'],
			'codesn'=>$post['codesn'],
			'alipay'=>$post['alipay'],
			'bankname'=>$post['bankname'],
			'bankcard'=>$post['bankcard'],
			'wechat'=>$post['wechat'],
			'mobile'=>$post['mobile'],
			'createtime'=>TIMESTAMP,
			'status'=>0,
		);
		//结算手续费
		if($this->setting['balance_per'] && ($post['pay_type']!='credit' || !$this->setting['balance_credit'])){
			$new_balance['balance_pay']=intval($new_balance['money']*$this->setting['balance_per'])/100;
		}else{
			$new_balance['balance_pay']=0;
		}
		$new_balance['final_money']=$new_balance['money']-$new_balance['balance_pay'];
		if($post['pay_type']=='credit' && $this->setting['balance_creditnocheck']){
			$new_balance['status']=3;
			$new_balance['pay_time']=TIMESTAMP;
			$this->pay_balance_by_credit($mid,$new_balance['final_money']);
		}
		$this->insert('balance',$new_balance);
		$balance_id=pdo_insertid();
		pdo_update('jy_commission_member',array('money -='=>$money),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		if($post['pay_type']=='credit'){
			$typetitle='余额';
		}
		if($post['pay_type']=='wechat'){
			$typetitle='微信';
		}
		$this->model('notice')->notice_fenxiao_tixian($mid,$money,$typetitle);
		return $balance_id;
	}
	/*
		审核结算单
	*/
	public function check_balance($balance_id,$status,$remark=''){
		$update=array(
			'status'=>$status,
			'check_time'=>TIMESTAMP,
			'remark'=>$remark,
		);
		$this->update('balance',$update,array('id'=>$balance_id));
		return true;
		/*$update=array(
			'status'=>1,
			'balancesn'=>'',
		);
		$balance=$this->get('balance',array('id'=>$balance_id),array('mid','balancesn'));
		if($balance['balancesn']){
			$this->update('commission',$update,array('uniacid'=>$_SESSION['uniacid'],'mid'=>$balance['mid'],'balancesn'=>$balance['balancesn']));
		}*/
		return true;
	}
	/*
		打款结算单
	*/
	public function pay_balance($balance_id,$pay_type='',$wxgid='',$pay_trade_no=''){
		$balance=$this->get('balance',array('id'=>$balance_id,'uniacid'=>$_SESSION['uniacid']));
		if(!$balance || $balance['status']!=1){
			exi('结算单不存在或者不符合打款条件，打款失败！','error');
		}
		$pay_types=array_keys($this->pay_types);
		if(!$pay_type || !in_array($pay_type,$pay_types)){
			//exi('未设置打款方式或者打款方式不合法，打款失败！','error');
		}
		$func_name='pay_balance_by_'.$pay_type;
		$trade_no=$this->$func_name($balance['mid'],$balance['final_money'],$balance['balancesn'],$wxgid);
		$update=array(
			'status'=>3,
			'pay_trade_no'=>$trade_no=='pay_trade_no'?$pay_trade_no:$trade_no,
			'pay_time'=>TIMESTAMP,
		);
		if($pay_type){
			$update['pay_type']=$pay_type;
		}
		$this->update('balance',$update,array('id'=>$balance['id']));
		$this->model('notice')->notice_fenxiao_dakuan($balance['mid'],$balance['final_money']);
		return true;
	}
	/*
		线下打款
	*/
	protected function pay_balance_by_downline(){
		return 'pay_trade_no';
	}
	/*
		打款到余额
	*/
	protected function pay_balance_by_credit($mid,$money,$balancesn=''){
		return cfc('core')->member_credit($mid,'credit2',$money,true,'jy_commission','分销佣金打款');
	}
	/*
		打款到微信零钱
	*/
	protected function pay_balance_by_wechat($mid,$money,$balancesn='',$wxgid=''){
		if($wxgid){
			$wxapp=pdo_get('core_account_wxapp',array('id'=>$wxgid,'uniacid'=>$_SESSION['uniacid']),array('appid','mchid','paykey'));
		}
		$params=array(
			'appid'=>$wxapp['appid']?:'',
			'mch_id'=>$wxapp['mchid']?:'',
			'openid'=>$mid,
			'mch_billno'=>$balancesn,
			'money'=>$money,
			'wxgid'=>$wxgid,
			'remark'=>'代理商佣金发放',
		);
		$res=cfc('wechat_pay')->mch_pay($params);
		if(!$res['payment_no'] && !$res['batch_id']){
			exi('打款到微信零钱失败！','error');
		}
		return $res['payment_no']?:$res['batch_id'];
	}
	/*
		打款到支付宝
	*/
	protected function pay_balance_by_alipay($mid,$money,$balancesn=''){
		return 'pay_trade_no';
		exi('暂不支持打款到支付宝！','error');
	}
	/*
		打款到银行账户
	*/
	protected function pay_balance_by_union($mid,$money,$balancesn=''){
		$item['money']=$money;
		$item['mid']=$mid;
		$item['ordersn']=$balancesn;
		model('core/member/cash')->apitixian($item);
		return 'pay_trade_no';
		exi('暂不支持打款到银行账户！','error');
	}

	public function commission_remark($remark){
		if(!$remark){
			return '';
		}
		$remark=json_decode($remark,true);
		$goods_str='';
		if($remark['goods_commission']){
			foreach($remark['goods_commission'] as $goods){
				$goods['title']=pdo_getcolumn('jy_weishop_goods',array('id'=>$goods['goods_id']),'title');
				$goods_str.="商品ID【{$goods['goods_id']}】，商品名称【{$goods['title']}】，商品数量【{$goods['total']}】，商品总价【{$goods['total_price']}】，佣金规则【{$goods['commission_per']}】，分销佣金【{$goods['commission_money']}】\n";
			}
		}else{
			$goods_str='未启用';
		}
		$remark['level_title']=pdo_getcolumn('jy_commission_level',array('uniacid'=>$_SESSION['uniacid'],'level'=>$remark['commission_level']),'title');
		$remark['level_title']=$remark['level_title']?:'默认等级';
		$remark_str=<<<TPL
分销商等级ID【{$remark['level_id']}】，等级名称【{$remark['level_title']}】，佣金等级【{$remark['commission_level']}】，订单总价【{$remark['order_price']}】
商品独立佣金：
{$goods_str}
订单佣金：
除去设置独立佣金的商品外订单金额【{$remark['order_commission']['order_price']}】，佣金规则【{$remark['order_commission']['commission_per']}%】，分销佣金【{$remark['order_commission']['commission_money']}】
佣金总计：【{$remark['commission_money']}】
TPL;
		return $remark_str;
	}
	/*
		获取单独商品佣金
	*/
	protected function get_goodsfee($goodsid,$orderid,$ordergoodsid=''){
		$ordersn=pdo_getcolumn('jy_weishop_order',array('id'=>$orderid,'uniacid'=>$_SESSION['uniacid']),'ordersn');
		$commissionremark=$this->pdo_getcolumn('commission',array('ordersn'=>$ordersn,'uniacid'=>$_SESSION['uniacid']),'remark');
		//print_r($ordersn.'~~'.$commissionremark);exit;
		$commissionremark=json_decode($commissionremark,true);
		$goodsfee=0;
		if($commissionremark['goods_commission']){
			foreach($commissionremark['goods_commission'] as $gv){
				if($ordergoodsid){
					if($ordergoodsid==$gv['ordergoodsid']){
						$goodsfee +=$gv['commission_money'];
					}
				}else{
					if($goodsid==$gv['goods_id']){
						$goodsfee +=$gv['commission_money'];
					}
				}
			}
		}
		if($commissionremark['order_commission'] && $goodsfee==0){
			$allorderfee=pdo_getsum('jy_weishop_order_goods',array('orderid'=>$orderid,'goodsid'=>$goodsid),'total_price');
			//exi($allorderfee.'~'.$commissionremark['order_commission']['order_price'].'~'.$commissionremark['order_commission']['commission_money'],'error');
			$goodsfee=($allorderfee/$commissionremark['order_commission']['order_price'])*$commissionremark['order_commission']['commission_money'];
		}
		$goodsfee=intval($goodsfee*100)/100;
		return $goodsfee;
	}
	/*
		获取订单佣金
	*/
	protected function get_orderfee($orderid=0){
		$ordersn=pdo_getcolumn('jy_weishop_order',array('id'=>$orderid,'uniacid'=>$_SESSION['uniacid']),'ordersn');
		$money=$this->pdo_getsum('commission',array('ordersn'=>$ordersn,'uniacid'=>$_SESSION['uniacid']),'money');
		return $money;
	}
}